package cn.base.web.support.sysenum;

import cn.base.web.cahe.DefaultCacheService;
import cn.rengy.isql.Dao;
import cn.rengy.isql.GetSqlService;
import cn.rengy.isql.SqlEntity;
import com.baomidou.dynamic.datasource.annotation.DS;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

@Service
@Slf4j
@DS("admin")
public class SysCodeService {
	@Autowired(required = false)
	private Dao dao;
	@Autowired
	private DefaultCacheService cacheService;


	
	/*@Cacheable(value="testCache",key="#key1")
	public List<Map<String, Object>> test1(String key1){
		logger.error("已执行");
		List<Map<String, Object>> list=new ArrayList<Map<String, Object>>();
		Map<String, Object> m=new HashMap<String,Object>();
		list.add(m);
		return list;
	}*/


	/**
	 * 查询多个key
	 * @param codeKey
	 * @return
	 */
	public List<List<Map<String, Object>>> querys(String... codeKey){
		List<List<Map<String, Object>>> data=new ArrayList<>(codeKey.length);
		for(String key:codeKey){
			data.add(query(key));
		}
		return data;
	}

	private static final String sql="select code_name,code_value from sys_enum where is_disabled=0 and code_key=? order by order_no asc";
	public List<Map<String, Object>> query(String codeKey){
        try {
            return cacheService.getCache("sysCodeCache", codeKey, new Callable<List<Map<String, Object>>>() {

                @Override
                public List<Map<String, Object>> call() throws Exception {
                    List<Map<String, Object>> list= dao.queryForList(sql, codeKey);
                    return list;
                }

            });
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

		/*if(list!=null){
			map=new HashMap<String,Object>();
			map.put("list",list);
			Map<String,Object> codeMap=new HashMap<String,Object>();
			for(Map<String, Object> codemap:list){
				codeMap.put((String)codemap.get("code_value"), codemap.get("code_name"));
			}
			map.put("map",codeMap);
		}
		return map;*/
	}
	
//	public static String getCodeName(String codeValue,List<Map<String, Object>> codeList){
//		String codeName=codeValue;
//		if(codeList!=null){
//			for(Map<String, Object> code:codeList){
//				String code_value = (String)code.get("codeValue");
//				String code_name = (String)code.get("codeName");
//				if(code_value!=null&&code_value.equals(codeValue)){
//					codeName=code_name;
//					break;
//				}
//			}
//		}
//		return codeName;
//	}
//	public static String getCodeNamev2(String codeValue,List<Map<String, Object>> codeList){
//		String codeName=codeValue;
//		if(codeList!=null){
//			for(Map<String, Object> code:codeList){
//				String code_value = (String)code.get("value");
//				String code_name = (String)code.get("text");
//				if(code_value!=null&&code_value.equals(codeValue)){
//					codeName=code_name;
//					break;
//				}
//			}
//		}
//		return codeName;
//	}
	/*@Cacheable(value="tableCodeCache",key="#tablename")
	public Map<String,List<Map<String, Object>>> queryTableCode(String tablename){
		Map<String,List<Map<String, Object>>> map=null;
		List<Map<String, Object>> columnList=this.dbService.queryListBySqlId("select_code_by_tablename", tablename);
		if(columnList!=null){
			map=new HashMap<String,List<Map<String, Object>>>();
			for(Map<String, Object> columnMap:columnList){
				String column=(String)columnMap.get("table_column");
				String codeKey=(String)columnMap.get("code_key");
				List<Map<String, Object>> codeList=queryList(codeKey);
				map.put(column,codeList);
			}
		}
		return map;
	}*/
}
